package p_oracle;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Select extends JInternalFrame {
	Connection conn;
	DefaultTableModel model = new DefaultTableModel();
	DatabaseTest main;
	
	private JPanel panel;
	private JButton btnNewButton;
	private JTextField sql;
	private JScrollPane scrollPane;
	private JTable table;
	private JLabel status;
	public Select(Connection c, DatabaseTest d) {
		super("조회",true, true, true, true);
		setDefaultCloseOperation(JInternalFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 433, 282);

		this.conn = c;
		this.main = d;
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);
		getContentPane().add(getStatus(), BorderLayout.SOUTH);
	}
	
	public void setList(){
		String s = sql.getText();
		try{
			PreparedStatement pstmt = conn.prepareStatement(s);
			ResultSet rs = pstmt.executeQuery();
			ResultSetMetaData rsm = rs.getMetaData();
			Vector v = new Vector();
			v.add("NO");
			for(int i=1 ; i<=rsm.getColumnCount() ; i++){
				v.add(rsm.getColumnName(i));
			}
			model = new DefaultTableModel(v,0);
			table.setModel(model);
			int cnt = 1;
			while(rs.next()){
				Vector d = new Vector<>();
				d.add(cnt);
				for(int i=1 ; i<=rsm.getColumnCount() ; i++){
					d.add(rs.getString(i));
				}
				model.addRow(d);
				cnt++;
			}
			
		}catch(Exception ex){
			//ex.printStackTrace();
			status.setText("SQL에 오류가 있거나 테이블이 존재하지 않습니다.");
		}
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getBtnNewButton(), BorderLayout.EAST);
			panel.add(getSql(), BorderLayout.CENTER);
		}
		return panel;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("쿼리실행");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setList();
				}
			});
		}
		return btnNewButton;
	}
	private JTextField getSql() {
		if (sql == null) {
			sql = new JTextField();
			sql.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent ev) {
					if(ev.getKeyCode() == KeyEvent.VK_ENTER) setList();
				}
			});
			sql.setText("select * from ptbl");
			sql.setColumns(10);
		}
		return sql;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent ev) {
					if( main.update == null){
						main.openUpdate();
					}

					int row = table.getSelectedRow();
					String s = (String)table.getValueAt(row,1);
					String c = (String)table.getValueAt(row,2);
					String e = (String)table.getValueAt(row,3);
					
					main.update.setData(s, c, e);
					main.update.setVisible(true);
					main.update.toFront();
				}
			});
		}
		return table;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("쿼리를 실행하세요.");
		}
		return status;
	}
}
